package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.ResultHopeAdapter;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CryHopeResultActivity extends BaseActivity implements View.OnClickListener{

    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String id;
    private JSONArray dataArray;
    private String amount;
    private String createTime;
    private String nickname;
    private String avatar;
    private TextView hopetype;
    private TextView money;
    private List<HashMap<String,String>>dataList;
    private HashMap<String,String> dataMap;
    private ListView listView;
    private ImageView resultback;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:

                    break;
            }
        }
    };
    private EditText inwish;
    private Button sengwish;
    private String wish;
    private String amount1;
    private String blessList;
    private String failure;
    private String longitude;
    private String latitude;
    private String type;
    private TextView moneytype;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cry_hope_result);

        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid","1");
        userId = sharedPreferences.getString("userId", "0");
        token = sharedPreferences.getString("token", "2");
        longitude = sharedPreferences.getString("longitude", "");
        latitude = sharedPreferences.getString("latitude", "");
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        type = intent.getStringExtra("type");
        initResultView();
        if (type.equals("1")){
            hopetype.setText("手气红包");
        }else if (type.equals("2")){
            hopetype.setText("均分红包");
        }else if (type.equals("3")){
            hopetype.setText("一心红包");
        }else if (type.equals("4")){
            hopetype.setText("二意红包");
        }else {
            hopetype.setText("定制红包");
        }
        dataList = new ArrayList<>();
        getMoney();


    }

    private void initResultView() {
        hopetype = (TextView) findViewById(R.id.result_hopetype);
        money = (TextView) findViewById(R.id.result_moneytext);
        listView = (ListView) findViewById(R.id.result_usernumber);
        resultback = (ImageView) findViewById(R.id.cryhope_result_back);
        inwish = (EditText) findViewById(R.id.result_editinwish);
        sengwish = (Button) findViewById(R.id.result_sendwish);
        moneytype = (TextView) findViewById(R.id.result_moneytype);
        backmain = (ImageView) findViewById(R.id.cryresult_backmain);
        backmain.setOnClickListener(this);
        resultback.setOnClickListener(this);
        sengwish.setOnClickListener(this);

    }

    private void getMoney() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.CRY_RESULT);
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bonusId",id);
        jsonObject.put("longitude",longitude);
        jsonObject.put("latitude",latitude);
        params.setBodyContent(jsonObject.toJSONString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                failure = obj.getString("failure");
                String data = obj.getString("data");
                if (data==null){
                    getHopeState(failure);
                }else {
                    com.alibaba.fastjson.JSONObject obj2 = JSON.parseObject(data);
                    amount1 = obj2.getString("amount");
                    blessList = obj2.getString("blessList");
                    dataArray = JSONArray.parseArray(blessList);
                    for (int i = 0; i < dataArray.size(); i++) {
                        dataMap = new HashMap<String, String>();
                        Object o = dataArray.get(i);
                        com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(String.valueOf(o));
                        amount = obj1.getString("amount"); // 钱数
                        createTime = obj1.getString("createTime");// 时间
                        nickname = obj1.getString("nickname");//昵称
                        avatar = obj1.getString("avatar");//头像
                        wish = obj1.getString("wish");
                        dataMap.put("amount", amount);
                        dataMap.put("createTime", createTime);
                        dataMap.put("nickname", nickname);
                        dataMap.put("avatar", avatar);
                        dataMap.put("wish", wish);
                        dataList.add(dataMap);
                        ResultHopeAdapter adapter = new ResultHopeAdapter(CryHopeResultActivity.this, dataList);
                        listView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }

                    //money.setText(amount1);
                    if (amount1.equals("0.00")){
                        money.setText("");
                        moneytype.setText("白板了 不要灰心 我看好你哦");
                    }else {
                        money.setText(amount1);
                    }
                }

              

               // handler.sendEmptyMessage(1);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(CryHopeResultActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void getHopeState(String failure) {
        if (failure.equals("1024")){
            Toast.makeText(CryHopeResultActivity.this, "不要剁手，下波再来", Toast.LENGTH_SHORT).show();
        }else if (failure.equals("1020")){
            Toast.makeText(CryHopeResultActivity.this, "大胸弟,发一个二意红包吧", Toast.LENGTH_SHORT).show();
        }else if (failure.equals("1019")){
            Toast.makeText(CryHopeResultActivity.this, "大兄弟,发一个一心红包再抢吧", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cryhope_result_back:
                finish();
                break;
            case R.id.result_sendwish:
                sengWish();
            break;
            case R.id.cryresult_backmain:
                startActivity(new Intent(CryHopeResultActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }
    }
        // 发送祝福语
    private void sengWish() {
        String wish = String.valueOf(inwish.getText());
        RequestParams requestParams = new RequestParams(InternetAddressUtis.Url+InternetAddressUtis.RESULT_SENDWISH);
        requestParams.addHeader("Content-Type","application/json");
        requestParams.addHeader("token", token);
        requestParams.addHeader("appId", appId);
        requestParams.addHeader("userId", userId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bonusId",id);
        jsonObject.put("wish",wish);
        requestParams.setBodyContent(jsonObject.toJSONString());
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(CryHopeResultActivity.this,ex);            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
